<template>
  <div>
    <el-dialog
      title="编辑海报"
      :visible.sync="editPosterDialog"
      width="30%"
      @close="close"
    >
      <el-form ref="form" :rules="rules" :model="formData" label-width="80px">
        <el-form-item label="海报名称" prop="name">
          <el-input style="" v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item label="背景图" prop="file">
          <el-upload
            v-model="formData.file"
            action="#"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-change="handleChange"
            :on-remove="handleRemove"
            :file-list="fileList"
            :limit="1"
            :auto-upload="false"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <div class="smalltip">
            <i class="el-icon-info"></i>
            建议尺寸1080*1920
          </div>
        </el-form-item>
        <!-- <el-form-item label="排序" prop="sortId">
          <el-input v-model.number="formData.sortId"></el-input>
        </el-form-item> -->
        <el-form-item label="是否显示" prop="isDisplay">
          <el-switch v-model.number="formData.isDisplay"></el-switch>
        </el-form-item>
        <el-form-item label="">
          <el-row :gutter="10">
            <el-col :span="6">
              <el-button type="primary" @click="btnOk">保存</el-button>
            </el-col>
            <el-col :span="6">
              <el-button @click="close">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>

<script>
import { updatePosterByIdApi } from "@/api/application";
export default {
  props: {
    editPosterDialog: {
      type: Boolean,
    },
  },
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
      formData: {
        file: "",
        sortId: 0,
        name: "",
        type: 2,
        isDisplay: "",
      },
      fileList: [],
      rules: {
        file: [
          { required: true, validator: this.validateImage, trigger: "change" },
        ],
        sortId: [{ required: true, message: "请输入排序", trigger: "blur" }],
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        type: [{ required: true, message: "请输入排序", trigger: "blur" }],
        isDisplay: [
          { required: true, message: "请选择是否显示", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    // 图片校验
    validateImage(rule, value, callback) {
      const valid = this.fileList.length > 0;
      if (!valid) {
        callback(new Error("请选择图片"));
      } else {
        callback();
      }
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    // 选择图片
    handleChange(file, fileList) {
      console.log(file);
      this.formData.file = file.raw;
      console.log(fileList);
      this.fileList = fileList.map((item) => item);
      console.log(this.fileList);
    },
    close() {
      this.$emit("update:editPosterDialog", false);
    },
    // 保存
    btnOk() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          try {
            const formData = new FormData();
            formData.append("file", this.formData.file);
            formData.append("name", this.formData.name);
            formData.append("type", this.formData.type);
            formData.append("sortId", this.formData.sortId);
            formData.append(
              "isDisplay",
              this.formData.isDisplay === true ? 1 : 0
            );
            const res = await updatePosterByIdApi(formData, this.formData.id);
            console.log(res);
            if (res.code === 1) {
              this.$message.success("保存成功");
              this.$emit("update:editPosterDialog", false);
              this.$parent.selPosterList();
            } else {
              this.$message.error("保存失败");
            }
          } catch {
            this.$message.error("保存失败");
          }
        }
      });
    },
  },
};
</script>

<style scoped></style>
